package com.jinunn.user.service.impl;

import com.atguigu.yygh.enums.DictEnum;
import com.atguigu.yygh.model.user.Patient;
import com.atguigu.yygh.model.user.UserInfo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jinunn.common.utils.AuthContextHolder;
import com.jinunn.user.mapper.PatientMapper;
import com.jinunn.user.service.PatientService;
import com.jinunn.yygh.cmn.client.DictFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author : JinDun
 * @date : 2021/5/19 22:28
 */
@Service
public class PatientServiceImpl extends ServiceImpl<PatientMapper, Patient> implements PatientService {

    @Autowired
    private DictFeignClient dictFeignClient;

    @Override
    public List<Patient> findAll(HttpServletRequest request) {
        Long userId = AuthContextHolder.getUserId(request);
        List<Patient> patientList = baseMapper.selectList(new QueryWrapper<Patient>().eq("user_id", userId));
        patientList.forEach(this::getPatient);
        return patientList;
    }

    @Override
    public boolean savePatient(Patient patient, HttpServletRequest request) {
        Long userId = AuthContextHolder.getUserId(request);
        patient.setUserId(userId);
        int insert = baseMapper.insert(patient);
        return insert>0;
    }

    @Override
    public Patient patientDetail(Long id) {
        return this.getPatient(baseMapper.selectById(id));
    }

    @Override
    public boolean updatePatient(Patient patient) {
        int result = baseMapper.updateById(patient);
        return result>0;
    }

    @Override
    public boolean deletePatient(Long id) {
        int result = baseMapper.deleteById(id);
        return result>0;
    }

    @Override
    public List<Patient> findUserPatient(Integer userId) {
        QueryWrapper<UserInfo> wrapper = new QueryWrapper<>();
        List<Patient> patientList = baseMapper.selectList(new QueryWrapper<Patient>().eq("user_id", userId));
        patientList.forEach(this::getPatient);
        return patientList;
    }

    private Patient getPatient(Patient patient) {
        //根据证件类型编码，获取证据具体类型
        String certificatesTypeName = dictFeignClient
                .getDictName(DictEnum.CERTIFICATES_TYPE.getDictCode(), patient.getCertificatesType());
        //联系人证件类型
        String contactsCertificatesTypeName = dictFeignClient
                .getDictName(DictEnum.CERTIFICATES_TYPE.getDictCode(), patient.getContactsCertificatesType());
        //省
        String province = dictFeignClient.getDictName(patient.getProvinceCode());
        //市
        String city = dictFeignClient.getDictName(patient.getCityCode());
        //区
        String district = dictFeignClient.getDictName(patient.getDistrictCode());

        patient.getParam().put("certificatesTypeName", certificatesTypeName);
        patient.getParam().put("contactsCertificatesTypeName", contactsCertificatesTypeName);
        patient.getParam().put("province", province);
        patient.getParam().put("city", city);
        patient.getParam().put("district", district);
        patient.getParam().put("fullAddress", province + city + district + patient.getAddress());
        return patient;
    }
}
